//
//  ViewController.m
//  核心动画
//
//  Created by MyMac on 15/6/6.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "ViewController.h"
#import "PushViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIImageView*imageView;
@property(nonatomic,strong)UIButton*Bview;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
       [self.view addSubview:self.imageView];
    
        UIButton*view=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
        [view addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
        view.backgroundColor=[UIColor redColor];//动画期间可以交互
    self.Bview=view;
        [self.view addSubview:_Bview];
    
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//
//  [self BaseAnnimation:_Bview];
    static int i=1;
    if(i==2){
        i--;
    }else
    {
        i++;
    }
    NSString*image=[NSString stringWithFormat:@"%d.png",i];
    _imageView.image=[UIImage imageNamed:image];
      [self TransitionAnnimation:_imageView];
//    
    
    
//   [self KeyAnnimation];
   
    
    
}
-(void)TransitionAnnimation:(UIImageView*)view
{
    CATransition*transition=[CATransition animation];
    transition.type=@"cube";
    transition.subtype=kCATransitionFromRight;
 transition.duration=1.0;
    [view.layer addAnimation:transition forKey:nil];
//    PushViewController*push=[[PushViewController alloc]init];
//  [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];//过度动画要加到两个过度视图的公共平台，模态动画的公共平台就是window，，，，push动画的公共平台就是导航控制器
//    [self presentViewController:push animated:YES completion:nil];
//    [CATransaction begin];
//    CATransition *transition = [CATransition animation];
//    transition.type = @"cube";
//    transition.duration = 0.5f;
//    transition.fillMode = kCAFillModeForwards;
//    transition.removedOnCompletion = YES;
//    
//    [[UIApplication sharedApplication].keyWindow.layer addAnimation:transition forKey:@"transition"];
//    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
//    [CATransaction setCompletionBlock: ^ {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(transition.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^ {
//            [[UIApplication sharedApplication] endIgnoringInteractionEvents];
//        });
//    }];
//    
//    [self presentViewController:push animated:YES completion:nil];
//    
//    [CATransaction commit];




}
-(void)KeyAnnimation
{
    CAKeyframeAnimation*anim=[CAKeyframeAnimation animationWithKeyPath:@"position"];//表示物体按照一串的数据动画，，也就是可以控制position这个属性运行时的每一步
   // anim.keyPath=@"position";
    anim.removedOnCompletion=NO;
    anim.fillMode=kCAFillModeForwards;
    anim.duration=2.0;
    
    
//    CGMutablePathRef path=CGPathCreateMutable();//创建了一个路径
//    CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));//这个路径是椭圆型的

    
    CGMutablePathRef path = CGPathCreateMutable();
    //将路径的起点定位到
   // （50  120）
    CGPathMoveToPoint(path,NULL,100,100);
    //下面5行添加5条直线的路径到path中
    CGPathAddLineToPoint(path,
                         NULL, 60, 130);
    CGPathAddLineToPoint(path,
                         NULL, 70, 140);
    CGPathAddLineToPoint(path,
                         NULL, 80, 150);
    CGPathAddLineToPoint(path,
                         NULL, 90, 160);
    CGPathAddLineToPoint(path,
                         NULL, 100, 170);
    //下面四行添加四条曲线路径到path
    CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
    CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
    CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
    CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,20,300);
    //以“position”为关键字
    
        anim.path=path;//表示物体按照path这个路径动画，，默认为nil，设置后value属性会被覆盖,,,,也可以用values属性完成
        CGPathRelease(path);
  //  anim.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];//设置运行的时间功能，，也就是不同阶段的运行速度,,,先慢  中间快，，最后慢
    anim.delegate=self;
    [self.Bview.layer addAnimation:anim forKey:nil];
//    设定每一帧的时间
//    
//    默认情况下,一帧动画的播放,分割 的时间是动画的总时间除以帧数减去一。你可以通过下面的公式决定每帧动画的时间:总时间/(总帧数-1)。 例如,如果你指定了一个 5 帧,10 秒的动画,那么每帧的时间就是 2.5 秒钟:10/(5-1)=2.5。你可以做更多 的控制通过使用 keyTimes 关键字,你可以给每帧动画指定总时间之内的某个时间点。
//    
//    通过设置属性keyTimes，能实现这个功能，这个属性是一个数组，其成员必须是NSNumber。
//    
//    同时 这个属性的设定值要与calculationMode属性相结合，同时他们有一定的规则，


}
-(void)BaseAnnimation:(UIButton*)view
{
  
    CAAnimationGroup*group=[CAAnimationGroup animation];
  
    CABasicAnimation*rotation=[CABasicAnimation animation];
    rotation.keyPath=@"transform.rotation";
    rotation.toValue=@(M_PI_2);
  //  rotation.duration=2.0;
    
    CABasicAnimation*posation=[CABasicAnimation animation];
    posation.keyPath=@"position";
    posation.toValue=[NSValue valueWithCGPoint:CGPointMake(1000, 1000)];
  // posation.duration=2.0;
    
    
    CABasicAnimation*scale=[CABasicAnimation animation];
    scale.keyPath=@"transform.scale";
    scale.toValue=@(2.0);
   // scale.duration=2.0;
    
    
    group.animations=@[rotation,scale,posation];
    group.removedOnCompletion=NO;//动画完后不删除动画
    group.fillMode=kCAFillModeForwards;//保持动画后的状态
    group.duration=2.0;
    
    
    [view.layer addAnimation:group  forKey:nil];



}
-(void)btn
{

    NSLog(@"111111");

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
